package cc.rengu.igas.share.biz.service.impl;

import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.igas.share.facade.UpqcUnifiedService;
import cc.rengu.igas.share.facade.request.UpqcC2bAddDealRequest;
import cc.rengu.igas.share.facade.request.UpqcC2bTransNoticeRequest;
import cc.rengu.igas.share.facade.request.UpqcPayResultNoticeRequest;
import cc.rengu.igas.share.facade.request.UpqcQrCodeQueryRequest;
import cc.rengu.igas.share.facade.response.UpqcC2bAddDealResponse;
import cc.rengu.igas.share.facade.response.UpqcC2bTransNoticeResponse;
import cc.rengu.igas.share.facade.response.UpqcPayResultNoticeResponse;
import cc.rengu.igas.share.facade.response.UpqcQrCodeQueryResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 银联二维码业务统一处理服务
 * Created by wangcheng on 2021/9/29 15:56
 */
public class UpqcUnifiedServiceImpl implements UpqcUnifiedService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<UpqcC2bAddDealResponse> c2bAddDeal(UpqcC2bAddDealRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpqcC2bAddDealResponse> result = new Result<>();
        UpqcC2bAddDealResponse upqcC2bAddDealResponse = new UpqcC2bAddDealResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpqcC2bAddDealResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpqcC2bAddDealResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("主被扫银联通知需要输入密码处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upqcC2bAddDealResponse.setRespCode(((BizException) e).getCode());
                upqcC2bAddDealResponse.setRespDesc(e.getMessage());
            } else {
                upqcC2bAddDealResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                upqcC2bAddDealResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(upqcC2bAddDealResponse);
        }
        return result;
    }

    @Override
    public Result<UpqcC2bTransNoticeResponse> cb2TransNotice(UpqcC2bTransNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpqcC2bTransNoticeResponse> result = new Result<>();
        UpqcC2bTransNoticeResponse upqcC2bTransNoticeResponse = new UpqcC2bTransNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpqcC2bTransNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpqcC2bTransNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("主被扫银联支付成功通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upqcC2bTransNoticeResponse.setRespCode(((BizException) e).getCode());
                upqcC2bTransNoticeResponse.setRespDesc(e.getMessage());
            } else {
                upqcC2bTransNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                upqcC2bTransNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            upqcC2bTransNoticeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            upqcC2bTransNoticeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            result.setResult(upqcC2bTransNoticeResponse);
        }
        return result;
    }

    @Override
    public Result<UpqcPayResultNoticeResponse> payNotice(UpqcPayResultNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpqcPayResultNoticeResponse> result = new Result<>();
        UpqcPayResultNoticeResponse payResultNoticeResponse = new UpqcPayResultNoticeResponse();
        rglog.debug("收单侧银联二维码交易结果通知处理");
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpqcPayResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpqcPayResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联二维码收款通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                payResultNoticeResponse.setRespCode(((BizException) e).getCode());
                payResultNoticeResponse.setRespDesc(e.getMessage());
            } else {
                payResultNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                payResultNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            /*应用层无论处理成功与否，通讯层返回银联成功*/
            payResultNoticeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            payResultNoticeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            result.setResult(payResultNoticeResponse);
        }
        return result;
    }

    @Override
    public Result<UpqcQrCodeQueryResponse> qrCodeQuery(UpqcQrCodeQueryRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpqcQrCodeQueryResponse> result = new Result<>();
        UpqcQrCodeQueryResponse qrCodeQueryResponse = new UpqcQrCodeQueryResponse();
        rglog.debug("收单侧银联二维码订单查询");
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpqcQrCodeQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpqcQrCodeQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联二维码查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                qrCodeQueryResponse.setRespCode(((BizException) e).getCode());
                qrCodeQueryResponse.setRespDesc(e.getMessage());
            } else {
                qrCodeQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                qrCodeQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            rglog.debug("返回相应码<{}>,返回相应码<{}>", qrCodeQueryResponse.getRespCode(), qrCodeQueryResponse.getRespDesc());
            result.setResult(qrCodeQueryResponse);
        }
        return result;
    }
}
